package com.hlkj.action.login;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.xml.rpc.ServiceException;

import org.apache.axis.AxisFault;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.easy.tool.form.FormRead;
import com.easy.tool.results.JsonBulid;
import com.easy.tool.results.ResultParameters;
import com.hlkj.util.DBPoolUtil;
import com.hlkj.util.StringUtil;
import com.hlkj.webservice.SendMessage;

@SuppressWarnings("static-access")
public class LoginAction {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;

	protected Connection getConnection() throws SQLException {
		return DBPoolUtil.getConnection();
	}
	
	protected void closeConnection() throws SQLException {
		DBPoolUtil.close();
	}


	public String getUserInfo() {
		JsonBulid jsonBulid = null;
		try {
			// http://127.0.0.1:8080/struts2_jdbc_5_30/login/getUserInfo?phone_number=18994874626&&pwd=123456
			FormRead form = new FormRead();
			String phone_number = form.getParamValue("phone_number");
			String pwd = form.getParamValue("pwd");

			String sql = "SELECT user_id,user_name,phone_number,pwd,pwd_md5,head_picture "
					+ "FROM base_user "
					+ "WHERE phone_number = '"
					+ phone_number + "' AND pwd = '" + pwd + "'";
			if (phone_number != null && !"".equals(phone_number)) {
				conn = getConnection();
				ps = conn.prepareStatement(sql);
				rs = ps.executeQuery();
				jsonBulid = JsonBulid.success(true).msg("获取用户信息成功!");
				while (rs.next()) {
					jsonBulid.add("user_id", rs.getInt("user_id"));
					jsonBulid.add("user_name", rs.getString("user_name"));
					jsonBulid.add("phone_number", rs.getString("phone_number"));
					jsonBulid.add("pwd", rs.getString("pwd"));
					jsonBulid.add("pwd_md5", rs.getString("pwd_md5"));
					jsonBulid.add("head_picture", rs.getString("head_picture"));
				}
			} else {
				jsonBulid = jsonBulid.success(false).msg("获取登陆参数失败!");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			jsonBulid = jsonBulid.success(false).msg("获取用户信息失败!");
		} finally {
			try {
				closeConnection();
			} catch (SQLException e) {
				e.printStackTrace();
				jsonBulid = jsonBulid.success(false).msg("获取用户信息失败!");
			}
		}
		ResultParameters.setJson(jsonBulid.json());
		return ResultParameters.JSON;
	}

	public String getUserInfoJsonObject() {
		JSONObject jsonObject = new JSONObject();
		try {
			conn = getConnection();
			String sql = "SELECT user_id,user_name,phone_number,pwd,pwd_md5,head_picture "
					+ "FROM base_user "
					+ "WHERE phone_number = '18994874626' AND pwd = '123456'";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				jsonObject.put("user_id", rs.getInt("user_id"));
				jsonObject.put("user_name", rs.getString("user_name"));
				jsonObject.put("phone_number", rs.getString("phone_number"));
				jsonObject.put("pwd", rs.getString("pwd"));
				jsonObject.put("pwd_md5", rs.getString("pwd_md5"));
				jsonObject.put("head_picture", rs.getString("head_picture"));
			}
			jsonObject.put("success", true);
			jsonObject.put("msg", "获取用户信息成功!");
		} catch (SQLException e) {
			jsonObject.put("success", false);
			jsonObject.put("msg", "获取用户信息失败!");
			e.printStackTrace();
		} finally {
//			try {
//				conn.close();
//			} catch (SQLException e) {
//				e.printStackTrace();
//				jsonObject.put("success", false);
//				jsonObject.put("msg", "获取用户信息失败!");
//			}
		}
		ResultParameters.setJson(jsonObject);
		return ResultParameters.JSON;
	}

	public String login_jsp() {
		return "login_jsp";
	}

	public String getListUsers() {
		
		JsonBulid jsonBulid = null;
		try {
			// http://127.0.0.1:8080/struts2_jdbc_5_30/login/getListUsers
			String sql = "SELECT user_id,user_name,phone_number,pwd,pwd_md5,head_picture "
					+ "FROM base_user ";
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			JSONArray array = new JSONArray();
			while (rs.next()) {
				jsonBulid = JsonBulid.success(true).msg("获取成功!");
				jsonBulid.add("user_id", rs.getInt("user_id"));
				jsonBulid.add("user_name", rs.getString("user_name"));
				jsonBulid.add("phone_number", rs.getString("phone_number"));
				jsonBulid.add("pwd", rs.getString("pwd"));
				jsonBulid.add("pwd_md5", rs.getString("pwd_md5"));
				jsonBulid.add("head_picture", rs.getString("head_picture"));
				array.add(jsonBulid.json());
			}
			jsonBulid = JsonBulid.success(true).msg("获取成功!").add(
					"getUsersInfo", array);
		} catch (SQLException e) {
			e.printStackTrace();
			jsonBulid = jsonBulid.success(false).msg("获取失败!");
		} finally {
			try {
				closeConnection();
			} catch (SQLException e) {
				e.printStackTrace();
				jsonBulid = jsonBulid.success(false).msg("获取失败!");
			}
		}
		ResultParameters.setJson(jsonBulid.json());
		return ResultParameters.JSON;
	}
}
